import React, { Component } from 'react'
import { Menu } from 'antd';
import local from '../localData'
import { Link, HashRouter as Router } from "react-router-dom";
const { SubMenu } = Menu;
class Leftmenu extends Component {
    constructor(props) {
        super(props);
        this.state = {
            menulist: [],
        }
    }
    // 获取左侧菜单栏
    componentDidMount() {
        this.pageInfo()
    }
    pageInfo = () => {
        let menuOpen = this.props.info
        for (let item of local.lifemenus.data) {
            let select = item.children.find((value) => {
                return value.url === this.props.props.location.pathname && value
            })
            if (select) {
                this.props.openMenuBack(menuOpen)
                this.openMenu(select)
            }
            this.setState({ menulist: local.lifemenus.data })
        }
    }
    openMenu = (info) => {
        let menuOpen = this.props.info
        let isHave = menuOpen.open.findIndex((value) => {
            return value.id === info.id
        })
        menuOpen.two = info.id
        menuOpen.one =  ['one'+ info.pid]
        if (isHave >= 0) {
            menuOpen.idx = isHave
        } else {
            menuOpen.idx = menuOpen.open.length
            menuOpen.open.push(info)
        }
        this.props.openMenuBack(menuOpen)
    }
    openChange=(info)=>{
        let menuOpen = this.props.info
        menuOpen.one = info
        this.props.openMenuBack(menuOpen)
    }
    render() {
        let menuInfo = this.props.info;
        return (
            <Router>
                {this.state.menulist.length > 0 &&
                    <Menu forceSubMenuRender={true} mode="inline" defaultOpenKeys={menuInfo.one} openKeys={menuInfo.one} selectedKeys={['two' + menuInfo.two]} style={{ height: '100%', borderRight: 0 }} onOpenChange={this.openChange}>
                        {this.state.menulist.map((item) => {
                            return (
                                <SubMenu key={'one' + item.id} title={<span>{item.menuName}</span>}>
                                    {
                                        item.children.map((item1) => {
                                            return (<Menu.Item key={'two' + item1.id} onClick={this.openMenu.bind(this, item1)}><Link to={item1.url}>{item1.menuName}</Link></Menu.Item>);
                                        })
                                    }
                                </SubMenu>
                            );
                        })}
                    </Menu>
                }
            </Router>
        )
    }
}
export default Leftmenu